﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Htse.Blue.Models
{
	public static class PageLinqExtensions
	{
		public static PagedList<T> ToPagedList<T>
			 (
				  this IQueryable<T> allItems,
				  int? pageIndex,
				  int pageSize
			 )
		{
			return ToPagedList<T>(allItems, pageIndex, pageSize, string.Empty);

		}

		public static PagedList<T> ToPagedList<T>
			 (
				  this IQueryable<T> allItems,
				  int? pageIndex,
				  int pageSize,
				  string sort
			 )
		{
			return ToPagedList<T>(allItems, pageIndex, pageSize, sort, string.Empty);
		}

		public static PagedList<T> ToPagedList<T>
			 (
				  this IQueryable<T> allItems,
				  int? pageIndex,
				  int pageSize,
				  string sort,
				  string filter
			 )
		{
			var truePageIndex = pageIndex ?? 0;
			var itemIndex = truePageIndex * pageSize;
			var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
			var totalItemCount = allItems.Count();

			return new PagedList<T>(pageOfItems, truePageIndex, pageSize, totalItemCount, sort, filter);
		}
	}
}
